/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Log.Level;
/*     */ import com.zimbra.cs.mailclient.CommandFailedException;
/*     */ import com.zimbra.cs.mailclient.imap.CAtom;
/*     */ import com.zimbra.cs.mailclient.imap.Flags;
/*     */ import com.zimbra.cs.mailclient.imap.ImapConfig;
/*     */ import com.zimbra.cs.mailclient.imap.ImapConnection;
/*     */ import com.zimbra.cs.mailclient.imap.ImapRequest;
/*     */ import com.zimbra.cs.mailclient.imap.Literal;
/*     */ import com.zimbra.cs.mailclient.imap.MailboxName;
/*     */ import java.io.File;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.sql.Date;
/*     */ import org.junit.After;
/*     */ import org.junit.Before;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestImapThrottle
/*     */ {
/*     */   private static final String HOST = "localhost";
/*     */   private static final int PORT = 7143;
/*     */   private static final String USER = "imap-test-user";
/*     */   private static final String PASS = "test123";
/*  54 */   private int LOOP_LIMIT = LC.imap_throttle_command_limit.intValue();
/*     */   private ImapConnection connection;
/*     */   
/*     */   @Before
/*     */   public void createData() throws ServiceException, IOException
/*     */   {
/*  60 */     TestUtil.createAccount("imap-test-user");
/*  61 */     this.connection = connect();
/*     */   }
/*     */   
/*     */   @After
/*     */   public void clearData() throws ServiceException
/*     */   {
/*  67 */     ZMailbox mbox = TestUtil.getZMailbox("imap-test-user");
/*     */     
/*     */ 
/*  70 */     TestUtil.deleteAccount("imap-test-user");
/*  71 */     if (this.connection != null) {
/*  72 */       this.connection.close();
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void append() throws Exception {
/*  78 */     org.junit.Assert.assertTrue(this.connection.hasCapability("UIDPLUS"));
/*  79 */     Date date = new Date(System.currentTimeMillis());
/*  80 */     Flags flags = Flags.fromSpec("afs");
/*  81 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/*  82 */       Literal msg = message(100000);
/*     */       try {
/*  84 */         this.connection.append("INBOX", flags, date, msg);
/*     */       } finally {
/*  86 */         msg.dispose();
/*     */       }
/*     */     }
/*     */     
/*  90 */     Literal msg = message(100000);
/*     */     try {
/*  92 */       this.connection.append("INBOX", flags, date, msg);
/*  93 */       junit.framework.Assert.fail("expected exception here...");
/*     */     } catch (Exception e) {
/*  95 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     } finally {
/*  97 */       msg.dispose();
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void list() throws IOException {
/* 103 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 104 */       this.connection.list("", "*");
/*     */     }
/*     */     try
/*     */     {
/* 108 */       this.connection.list("", "*");
/* 109 */       junit.framework.Assert.fail("Expected exception here...");
/*     */     } catch (Exception e) {
/* 111 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void lsub() throws IOException {
/* 117 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 118 */       this.connection.lsub("", "*");
/*     */     }
/*     */     try
/*     */     {
/* 122 */       this.connection.lsub("", "*");
/* 123 */       junit.framework.Assert.fail("Expected exception here...");
/*     */     } catch (Exception e) {
/* 125 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void xlist() throws IOException {
/* 131 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 132 */       this.connection.newRequest(CAtom.XLIST, new Object[] { new MailboxName(""), new MailboxName("*") }).sendCheckStatus();
/*     */     }
/*     */     try
/*     */     {
/* 136 */       this.connection.newRequest(CAtom.XLIST, new Object[] { new MailboxName(""), new MailboxName("*") }).sendCheckStatus();
/* 137 */       junit.framework.Assert.fail("Expected exception here...");
/*     */     } catch (Exception e) {
/* 139 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void create()
/*     */     throws IOException
/*     */   {
/* 147 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 148 */       this.connection.create("foo" + i);
/* 149 */       if (i % 10 == 0) {
/*     */         try {
/* 151 */           Thread.sleep(250L);
/*     */         }
/*     */         catch (InterruptedException e) {}
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */     try
/*     */     {
/* 160 */       this.connection.create("overthelimit");
/* 161 */       junit.framework.Assert.fail("should be over consecutive create limit");
/*     */     } catch (CommandFailedException e) {
/* 163 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void store() throws IOException {
/* 169 */     Flags flags = Flags.fromSpec("afs");
/* 170 */     for (int i = 0; i < 3; i++) {
/* 171 */       Date date = new Date(System.currentTimeMillis());
/* 172 */       Literal msg = message(1000 + i * 1000);
/*     */       try {
/* 174 */         this.connection.append("INBOX", flags, date, msg);
/*     */       } finally {
/* 176 */         msg.dispose();
/*     */       }
/*     */     }
/* 179 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 180 */       this.connection.store("1:3", "FLAGS", new String[] { "FOO", "BAR" });
/*     */     }
/*     */     try
/*     */     {
/* 184 */       this.connection.store("1:3", "FLAGS", new String[] { "FOO", "BAR" });
/* 185 */       junit.framework.Assert.fail("should have been rejected");
/*     */     } catch (CommandFailedException e) {
/* 187 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void examine() throws IOException {
/* 193 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 194 */       this.connection.examine("INBOX");
/*     */     }
/*     */     try
/*     */     {
/* 198 */       this.connection.examine("INBOX");
/* 199 */       junit.framework.Assert.fail("should have been rejected");
/*     */     } catch (CommandFailedException e) {
/* 201 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void select() throws IOException {
/* 207 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 208 */       this.connection.select("SENT");
/*     */     }
/*     */     try
/*     */     {
/* 212 */       this.connection.select("SENT");
/* 213 */       junit.framework.Assert.fail("should have been rejected");
/*     */     } catch (CommandFailedException e) {
/* 215 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void fetch() throws IOException {
/* 221 */     Flags flags = Flags.fromSpec("afs");
/* 222 */     for (int i = 0; i < 3; i++) {
/* 223 */       Date date = new Date(System.currentTimeMillis());
/* 224 */       Literal msg = message(1000 + i * 1000);
/*     */       try {
/* 226 */         this.connection.append("INBOX", flags, date, msg);
/*     */       } finally {
/* 228 */         msg.dispose();
/*     */       }
/*     */     }
/* 231 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 232 */       this.connection.fetch(1L, new String[] { "FLAGS", "UID" });
/*     */     }
/*     */     try
/*     */     {
/* 236 */       this.connection.fetch(1L, new String[] { "FLAGS", "UID" });
/* 237 */       junit.framework.Assert.fail("should have been rejected");
/*     */     } catch (CommandFailedException e) {
/* 239 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void uidfetch() throws IOException {
/* 245 */     Flags flags = Flags.fromSpec("afs");
/* 246 */     for (int i = 0; i < 3; i++) {
/* 247 */       Date date = new Date(System.currentTimeMillis());
/* 248 */       Literal msg = message(1000 + i * 1000);
/*     */       try {
/* 250 */         this.connection.append("INBOX", flags, date, msg);
/*     */       } finally {
/* 252 */         msg.dispose();
/*     */       }
/*     */     }
/* 255 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 256 */       this.connection.uidFetch("1:*", new String[] { "FLAGS", "UID" });
/*     */     }
/*     */     try
/*     */     {
/* 260 */       this.connection.uidFetch("1:*", new String[] { "FLAGS", "UID" });
/* 261 */       junit.framework.Assert.fail("should have been rejected");
/*     */     } catch (CommandFailedException e) {
/* 263 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void copy() throws IOException {
/* 269 */     Flags flags = Flags.fromSpec("afs");
/* 270 */     for (int i = 0; i < 3; i++) {
/* 271 */       Date date = new Date(System.currentTimeMillis());
/* 272 */       Literal msg = message(1000 + i * 1000);
/*     */       try {
/* 274 */         this.connection.append("INBOX", flags, date, msg);
/*     */       } finally {
/* 276 */         msg.dispose();
/*     */       }
/*     */     }
/* 279 */     this.connection.create("FOO");
/* 280 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 281 */       this.connection.copy("1:3", "FOO");
/*     */     }
/*     */     try
/*     */     {
/* 285 */       this.connection.copy("1:3", "FOO");
/* 286 */       junit.framework.Assert.fail("should have been rejected");
/*     */     } catch (CommandFailedException e) {
/* 288 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void search() throws IOException {
/* 294 */     Flags flags = Flags.fromSpec("afs");
/* 295 */     for (int i = 0; i < 3; i++) {
/* 296 */       Date date = new Date(System.currentTimeMillis());
/* 297 */       Literal msg = message(1000 + i * 1000);
/*     */       try {
/* 299 */         this.connection.append("INBOX", flags, date, msg);
/*     */       } finally {
/* 301 */         msg.dispose();
/*     */       }
/*     */     }
/* 304 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 305 */       this.connection.search((Object[])new String[] { "TEXT", "\"XXXXX\"" });
/*     */     }
/*     */     try
/*     */     {
/* 309 */       this.connection.search((Object[])new String[] { "TEXT", "\"XXXXX\"" });
/* 310 */       junit.framework.Assert.fail("should have been rejected");
/*     */     } catch (CommandFailedException e) {
/* 312 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void sort() throws IOException {
/* 318 */     Flags flags = Flags.fromSpec("afs");
/* 319 */     for (int i = 0; i < 3; i++) {
/* 320 */       Date date = new Date(System.currentTimeMillis());
/* 321 */       Literal msg = message(1000 + i * 1000);
/*     */       try {
/* 323 */         this.connection.append("INBOX", flags, date, msg);
/*     */       } finally {
/* 325 */         msg.dispose();
/*     */       }
/*     */     }
/* 328 */     for (int i = 0; i < this.LOOP_LIMIT; i++) {
/* 329 */       this.connection.newRequest("SORT (DATE REVERSE SUBJECT) UTF-8 ALL", new Object[0]).sendCheckStatus();
/*     */     }
/*     */     try
/*     */     {
/* 333 */       this.connection.newRequest("SORT (DATE REVERSE SUBJECT) UTF-8 ALL", new Object[0]).sendCheckStatus();
/* 334 */       junit.framework.Assert.fail("should have been rejected");
/*     */     } catch (CommandFailedException e) {
/* 336 */       junit.framework.Assert.assertTrue(this.connection.isClosed());
/*     */     }
/*     */   }
/*     */   
/*     */   private static Literal message(int size) throws IOException {
/* 341 */     File file = File.createTempFile("msg", null);
/* 342 */     file.deleteOnExit();
/* 343 */     FileWriter out = new FileWriter(file);
/*     */     try {
/* 345 */       out.write(simpleMessage("test message"));
/* 346 */       for (int i = 0; i < size; i++) {
/* 347 */         out.write(88);
/* 348 */         if (i % 72 == 0) {
/* 349 */           out.write("\r\n");
/*     */         }
/*     */       }
/*     */     } finally {
/* 353 */       out.close();
/*     */     }
/* 355 */     return new Literal(file, true);
/*     */   }
/*     */   
/*     */   private static String simpleMessage(String text) {
/* 359 */     return "Return-Path: dac@zimbra.com\r\nDate: Fri, 27 Feb 2004 15:24:43 -0800 (PST)\r\nFrom: dac <dac@zimbra.com>\r\nTo: bozo <bozo@foo.com>\r\nSubject: Foo foo\r\n\r\n" + text + "\r\n";
/*     */   }
/*     */   
/*     */   private ImapConnection connect()
/*     */     throws IOException
/*     */   {
/* 365 */     ImapConfig config = new ImapConfig("localhost");
/* 366 */     config.setPort(7143);
/* 367 */     config.setAuthenticationId("imap-test-user");
/* 368 */     config.getLogger().setLevel(Log.Level.trace);
/* 369 */     ImapConnection connection = new ImapConnection(config);
/* 370 */     connection.connect();
/* 371 */     connection.login("test123");
/* 372 */     connection.select("INBOX");
/* 373 */     return connection;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestImapThrottle.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */